

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="python" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="python" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>QUANTAXIS.QAARP.QAPortfolio &mdash; QUANTAXIS 1.0.39 documentation</title>
  

  
  
  
  

  

  
  
    

  

  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> QUANTAXIS
          

          
          </a>

          
            
            
              <div class="version">
                1.0.39
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">QUANTAXIS</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>QUANTAXIS.QAARP.QAPortfolio</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for QUANTAXIS.QAARP.QAPortfolio</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding:utf-8</span>
<span class="c1">#</span>
<span class="c1"># The MIT License (MIT)</span>
<span class="c1">#</span>
<span class="c1"># Copyright (c) 2016-2018 yutiansut/QUANTAXIS</span>
<span class="c1">#</span>
<span class="c1"># Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="c1"># of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="c1"># in the Software without restriction, including without limitation the rights</span>
<span class="c1"># to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="c1"># copies of the Software, and to permit persons to whom the Software is</span>
<span class="c1"># furnished to do so, subject to the following conditions:</span>
<span class="c1">#</span>
<span class="c1"># The above copyright notice and this permission notice shall be included in all</span>
<span class="c1"># copies or substantial portions of the Software.</span>
<span class="c1">#</span>
<span class="c1"># THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="c1"># IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="c1"># FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<span class="c1"># AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="c1"># LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="c1"># OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE</span>
<span class="c1"># SOFTWARE.</span>

<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">lru_cache</span>

<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>

<span class="kn">from</span> <span class="nn">QUANTAXIS.QAARP.QAAccount</span> <span class="k">import</span> <span class="n">QA_Account</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAUtil</span> <span class="k">import</span> <span class="p">(</span><span class="n">DATABASE</span><span class="p">,</span> <span class="n">QA_util_log_info</span><span class="p">,</span>
                              <span class="n">QA_util_random_with_topic</span><span class="p">)</span>

<span class="c1"># pylint: disable=old-style-class, too-few-public-methods</span>


<div class="viewcode-block" id="QA_Portfolio"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QAPortfolio.QA_Portfolio">[docs]</a><span class="k">class</span> <span class="nc">QA_Portfolio</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;QA_Portfolio</span>
<span class="sd">    User--&gt;Portfolio--&gt;Account/Strategy</span>

<span class="sd">    ::::::::::::::::::::::::::::::::::::::::::::::::::</span>
<span class="sd">    ::        :: Portfolio 1 -- Account/Strategy 1  ::</span>
<span class="sd">    ::  USER  ::             -- Account/Strategy 2  ::</span>
<span class="sd">    ::        :: Portfolio 2 -- Account/Strategy 3  ::</span>
<span class="sd">    ::::::::::::::::::::::::::::::::::::::::::::::::::</span>

<span class="sd">    QUANTAXIS 多账户</span>
<span class="sd">    以及组合管理</span>

<span class="sd">    # 适用 回测/实盘</span>

<span class="sd">    # PORTFOLIO应当作为一个视图来处理,这个视图作为一个静态的观察点 可以去衡量风险 观察业绩等等</span>
<span class="sd">    @2018/02/26</span>

<span class="sd">    ::::::::::::::::::::::::::::::::::::::::::::::::</span>
<span class="sd">    ::        ::STRATEGY 1 -- ACCOUNT 1 --{P1,P3} ::</span>
<span class="sd">    ::  USER  ::STRATEGY 2 -- ACCOUNT 2 --{P1,P2} ::</span>
<span class="sd">    ::        ::STRATEGY 3 -- ACCOUNT 3 --{P2,P3} ::</span>
<span class="sd">    ::::::::::::::::::::::::::::::::::::::::::::::::</span>


<span class="sd">    PORTFOLIO</span>

<span class="sd">    在portfolio中,我们希望通过cookie来控制account_unit</span>

<span class="sd">    对于account的指标,要进行风险控制,组合成最优的投资组合的量</span>

<span class="sd">    用account的cookie来管理控制account</span>

<span class="sd">    portfolio里面的资产主要考虑的是 资金的分配</span>

<span class="sd">    @2018/05/16</span>
<span class="sd">    fix 通过 cookie 获取 account</span>

<span class="sd">    @royburns  1.根据指定的user_cookie创建user； 2.添加对应的测试代码； 3.添加注释</span>
<span class="sd">    2018/05/18</span>

<span class="sd">    @yutiansut</span>
<span class="sd">    修改init_assets ==&gt; init_cash ,删除cash,history在初始的输入</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">user_cookie</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">portfolio_cookie</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">strategy_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">init_cash</span><span class="o">=</span><span class="mi">1000000</span><span class="p">,</span> <span class="n">sell_available</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">user_cookie</span> <span class="o">=</span> <span class="n">user_cookie</span>
        <span class="c1"># self.portfolio_cookie = QA_util_random_with_topic(&#39;Portfolio&#39;)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">portfolio_cookie</span> <span class="o">=</span> <span class="n">QA_util_random_with_topic</span><span class="p">(</span>
            <span class="s1">&#39;Portfolio&#39;</span><span class="p">)</span> <span class="k">if</span> <span class="n">portfolio_cookie</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">portfolio_cookie</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">strategy_name</span> <span class="o">=</span> <span class="n">strategy_name</span>
        <span class="c1"># 和account一样的资产类</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">init_cash</span> <span class="o">=</span> <span class="n">init_cash</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cash</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">init_cash</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cash_available</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cash</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>  <span class="c1"># 可用资金</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sell_available</span> <span class="o">=</span> <span class="n">sell_available</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">history</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">time_index</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">cookie</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="n">cookie</span><span class="p">]</span> <span class="o">=</span> <span class="n">QA_Account</span><span class="p">(</span><span class="n">account_cookie</span><span class="o">=</span><span class="n">cookie</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;&lt; QA_Portfolio </span><span class="si">{}</span><span class="s1"> with </span><span class="si">{}</span><span class="s1"> Accounts &gt;&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">portfolio_cookie</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="o">.</span><span class="n">keys</span><span class="p">()))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">init_hold_table</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">account</span><span class="o">.</span><span class="n">init_hold_with_account</span> <span class="k">for</span> <span class="n">account</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="o">.</span><span class="n">values</span><span class="p">())])</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">init_hold</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">init_hold_table</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s1">&#39;code&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>

<div class="viewcode-block" id="QA_Portfolio.get_portfolio"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QAPortfolio.QA_Portfolio.get_portfolio">[docs]</a>    <span class="k">def</span> <span class="nf">get_portfolio</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s1">&#39;return the accounts dict&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span></div>

<div class="viewcode-block" id="QA_Portfolio.add_account"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QAPortfolio.QA_Portfolio.add_account">[docs]</a>    <span class="k">def</span> <span class="nf">add_account</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">):</span>
        <span class="s1">&#39;portfolio add a account/stratetgy&#39;</span>
        <span class="k">if</span> <span class="n">account</span><span class="o">.</span><span class="n">account_cookie</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="n">account</span><span class="o">.</span><span class="n">portfolio_cookie</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">portfolio_cookie</span>
            <span class="n">account</span><span class="o">.</span><span class="n">user_cookie</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">user_cookie</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="n">account</span><span class="o">.</span><span class="n">account_cookie</span><span class="p">]</span> <span class="o">=</span> <span class="n">account</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">pass</span></div>

<div class="viewcode-block" id="QA_Portfolio.new_account"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QAPortfolio.QA_Portfolio.new_account">[docs]</a>    <span class="k">def</span> <span class="nf">new_account</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account_cookie</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="s1">&#39;portfolio create a account/strategy&#39;</span>
        <span class="k">if</span> <span class="n">account_cookie</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">temp</span> <span class="o">=</span> <span class="n">QA_Account</span><span class="p">(</span><span class="n">portfolio_cookie</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">portfolio_cookie</span><span class="p">,</span>
                              <span class="n">user_cookie</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">user_cookie</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">temp</span><span class="o">.</span><span class="n">account_cookie</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="n">temp</span><span class="o">.</span><span class="n">account_cookie</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span>
                <span class="k">return</span> <span class="n">temp</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_account</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">account_cookie</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="n">account_cookie</span><span class="p">]</span> <span class="o">=</span> <span class="n">QA_Account</span><span class="p">(</span><span class="n">portfolio_cookie</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">portfolio_cookie</span><span class="p">,</span>
                                                           <span class="n">user_cookie</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">user_cookie</span><span class="p">,</span> <span class="n">account_cookie</span><span class="o">=</span><span class="n">account_cookie</span><span class="p">)</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="n">account_cookie</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">new_account</span><span class="p">(</span><span class="n">account_cookie</span><span class="p">)</span></div>

<div class="viewcode-block" id="QA_Portfolio.get_account_by_cookie"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QAPortfolio.QA_Portfolio.get_account_by_cookie">[docs]</a>    <span class="k">def</span> <span class="nf">get_account_by_cookie</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cookie</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        &#39;give the account_cookie and return the account/strategy back&#39;</span>
<span class="sd">        :param cookie:</span>
<span class="sd">        :return: QA_Account with cookie if in dict</span>
<span class="sd">                 None not in list</span>
<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="n">cookie</span><span class="p">]</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;Can not find this account&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">None</span></div>

<div class="viewcode-block" id="QA_Portfolio.get_account"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QAPortfolio.QA_Portfolio.get_account">[docs]</a>    <span class="k">def</span> <span class="nf">get_account</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        check the account whether in the protfolio dict or not</span>
<span class="sd">        :param account:  QA_Account</span>
<span class="sd">        :return: QA_Account if in dict</span>
<span class="sd">                 None not in list</span>
<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="n">account</span><span class="o">.</span><span class="n">account_cookie</span><span class="p">]</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="n">QA_util_log_info</span><span class="p">(</span>
                <span class="s1">&#39;Can not find this account with cookies </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">account</span><span class="o">.</span><span class="n">account_cookie</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">None</span></div>

<div class="viewcode-block" id="QA_Portfolio.cookie_mangement"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QAPortfolio.QA_Portfolio.cookie_mangement">[docs]</a>    <span class="k">def</span> <span class="nf">cookie_mangement</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">table</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">acc</span><span class="o">.</span><span class="n">table</span> <span class="k">for</span> <span class="n">acc</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="o">.</span><span class="n">values</span><span class="p">()],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

<div class="viewcode-block" id="QA_Portfolio.get_cash"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QAPortfolio.QA_Portfolio.get_cash">[docs]</a>    <span class="k">def</span> <span class="nf">get_cash</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;拿到整个portfolio的可用资金</span>

<span class="sd">        统计每一个时间点的时候的cash总和</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">pass</span></div>

<div class="viewcode-block" id="QA_Portfolio.pull"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QAPortfolio.QA_Portfolio.pull">[docs]</a>    <span class="k">def</span> <span class="nf">pull</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account_cookie</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">collection</span><span class="o">=</span><span class="n">DATABASE</span><span class="o">.</span><span class="n">account</span><span class="p">):</span>
        <span class="s1">&#39;pull from the databases&#39;</span>
        <span class="k">if</span> <span class="n">account_cookie</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">message</span> <span class="o">=</span> <span class="n">collection</span><span class="o">.</span><span class="n">find_one</span><span class="p">({</span><span class="s1">&#39;account_cookie&#39;</span><span class="p">:</span> <span class="n">item</span><span class="p">})</span>
                    <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> sync successfully&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">item</span><span class="p">))</span>
                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="n">QA_util_log_info</span><span class="p">(</span>
                        <span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> sync wrong </span><span class="se">\\\n</span><span class="s1"> wrong info </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="n">e</span><span class="p">))</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="n">item</span><span class="p">]</span><span class="o">.</span><span class="n">from_message</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">message</span> <span class="o">=</span> <span class="n">collection</span><span class="o">.</span><span class="n">find_one</span><span class="p">(</span>
                    <span class="p">{</span><span class="s1">&#39;account_cookie&#39;</span><span class="p">:</span> <span class="n">account_cookie</span><span class="p">})</span>
                <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> sync successfully&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">item</span><span class="p">))</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">QA_util_log_info</span><span class="p">(</span>
                    <span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> sync wrong </span><span class="se">\\\n</span><span class="s1"> wrong info </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">account_cookie</span><span class="p">,</span> <span class="n">e</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="n">account_cookie</span><span class="p">]</span><span class="o">.</span><span class="n">from_message</span><span class="p">(</span><span class="n">message</span><span class="p">)</span></div>

<div class="viewcode-block" id="QA_Portfolio.push"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QAPortfolio.QA_Portfolio.push">[docs]</a>    <span class="k">def</span> <span class="nf">push</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account_cookie</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">collection</span><span class="o">=</span><span class="n">DATABASE</span><span class="o">.</span><span class="n">account</span><span class="p">):</span>
        <span class="s1">&#39;push to databases&#39;</span>
        <span class="n">message</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="n">account_cookie</span><span class="p">]</span><span class="o">.</span><span class="n">message</span>
        <span class="k">if</span> <span class="n">account_cookie</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">message</span> <span class="o">=</span> <span class="n">collection</span><span class="o">.</span><span class="n">find_one_and_update</span><span class="p">(</span>
                        <span class="p">{</span><span class="s1">&#39;account_cookie&#39;</span><span class="p">:</span> <span class="n">item</span><span class="p">})</span>
                    <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> sync successfully&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">item</span><span class="p">))</span>
                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="n">QA_util_log_info</span><span class="p">(</span>
                        <span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> sync wrong </span><span class="se">\\\n</span><span class="s1"> wrong info </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="n">e</span><span class="p">))</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="n">item</span><span class="p">]</span><span class="o">.</span><span class="n">from_message</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">message</span> <span class="o">=</span> <span class="n">collection</span><span class="o">.</span><span class="n">find_one</span><span class="p">(</span>
                    <span class="p">{</span><span class="s1">&#39;account_cookie&#39;</span><span class="p">:</span> <span class="n">account_cookie</span><span class="p">})</span>
                <span class="n">QA_util_log_info</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> sync successfully&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">item</span><span class="p">))</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">QA_util_log_info</span><span class="p">(</span>
                    <span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> sync wrong </span><span class="se">\\\n</span><span class="s1"> wrong info </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">account_cookie</span><span class="p">,</span> <span class="n">e</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="n">account_cookie</span><span class="p">]</span><span class="o">.</span><span class="n">from_message</span><span class="p">(</span><span class="n">message</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="QA_TEST_MAKEPortfolio"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QAPortfolio.QA_TEST_MAKEPortfolio">[docs]</a><span class="k">class</span> <span class="nc">QA_TEST_MAKEPortfolio</span><span class="p">():</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        this is a dict for account_cookie----account instance</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">account_list</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>

<div class="viewcode-block" id="QA_TEST_MAKEPortfolio.make_portfolio"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QAPortfolio.QA_TEST_MAKEPortfolio.make_portfolio">[docs]</a>    <span class="k">def</span> <span class="nf">make_portfolio</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account_list</span><span class="p">):</span>
        <span class="k">pass</span></div></div>


<div class="viewcode-block" id="QA_PortfolioView"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAARP.html#QUANTAXIS.QAARP.QAPortfolio.QA_PortfolioView">[docs]</a><span class="k">class</span> <span class="nc">QA_PortfolioView</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    对于Portfolio而言,一切都是基于内部的account的信息的变更而变更的</span>

<span class="sd">    Portfolio不应该有过多可以修改的部分(作为一个view存在)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account_list</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">                    ||portfolio||</span>
<span class="sd">        ||acc1_cookie--acc1||acc2-cookie--acc2||...||</span>


<span class="sd">        ||cash||assets||hold||history||trade_index||</span>


<span class="sd">        ||Risk_analysis||Performace_analysis||</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">account_cookie</span> <span class="o">=</span> <span class="n">QA_util_random_with_topic</span><span class="p">(</span><span class="s1">&#39;PVIEW&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">account_list</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
            <span class="nb">zip</span><span class="p">([</span><span class="n">account</span><span class="o">.</span><span class="n">account_cookie</span> <span class="k">for</span> <span class="n">account</span> <span class="ow">in</span> <span class="n">account_list</span><span class="p">],</span> <span class="n">account_list</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">portfolio_cookie</span> <span class="o">=</span> <span class="n">QA_util_random_with_topic</span><span class="p">(</span><span class="s1">&#39;Portfolio&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">user_cookie</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;&lt; QA_PortfolioVIEW </span><span class="si">{}</span><span class="s1"> with </span><span class="si">{}</span><span class="s1"> Accounts &gt;&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account_cookie</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">contained_cookie</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>

<span class="sd">        CHANGED in 1.0.38</span>
<span class="sd">        2018-05-23</span>

<span class="sd">        portfolio-view 含有的account_cookie使用contained_cookie来承载</span>

<span class="sd">        原先的account_cookie 使用 PVIEW_xxx 代替</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">account</span><span class="o">.</span><span class="n">account_cookie</span> <span class="k">for</span> <span class="n">account</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">accounts</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        return all accounts inside the portfolio view</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">account_list</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">start_date</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="n">account</span><span class="o">.</span><span class="n">start_date</span> <span class="k">for</span> <span class="n">account</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">]))</span><span class="o">.</span><span class="n">min</span><span class="p">())[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">end_date</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="n">account</span><span class="o">.</span><span class="n">end_date</span> <span class="k">for</span> <span class="n">account</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">]))</span><span class="o">.</span><span class="n">max</span><span class="p">())[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">code</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">account</span><span class="o">.</span><span class="n">code</span><span class="p">)</span> <span class="k">for</span> <span class="n">account</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">])</span><span class="o">.</span><span class="n">drop_duplicates</span><span class="p">()</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">init_cash</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">sum</span><span class="p">([</span><span class="n">account</span><span class="o">.</span><span class="n">init_cash</span> <span class="k">for</span> <span class="n">account</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">])</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">daily_cash</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">res</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="nb">sum</span><span class="p">([</span><span class="n">account</span><span class="o">.</span><span class="n">daily_cash</span><span class="o">.</span><span class="n">set_index</span><span class="p">(</span>
            <span class="s1">&#39;datetime&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">cash</span> <span class="k">for</span> <span class="n">account</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">]))</span>
        <span class="n">res</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">date</span><span class="o">=</span><span class="n">res</span><span class="o">.</span><span class="n">index</span><span class="p">)</span>
        <span class="n">res</span><span class="o">.</span><span class="n">date</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">10</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">res</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">daily_hold</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">account</span><span class="o">.</span><span class="n">daily_hold</span><span class="o">.</span><span class="n">xs</span><span class="p">(</span><span class="n">account</span><span class="o">.</span><span class="n">account_cookie</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="k">for</span> <span class="n">account</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">])</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s1">&#39;date&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, yutiansut.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'1.0.39',
            LANGUAGE:'python',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>